package com.e_manager.soft.emanager.controller;

import com.e_manager.soft.emanager.annotation.Authorization;
import com.e_manager.soft.emanager.annotation.BaseController;
import com.e_manager.soft.emanager.annotation.CurrentUser;
import com.e_manager.soft.emanager.entity.User;
import com.e_manager.soft.emanager.entity.parameter.UserEquipmentParameter;
import com.e_manager.soft.emanager.resultMsg.ResultConstant;
import com.e_manager.soft.emanager.resultMsg.ResultInfo;
import com.e_manager.soft.emanager.service.UserService;
import com.e_manager.soft.emanager.token.TokenConstant;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author : lip[lip4051@163.com]
 * @create : 2018/3/13 0013 下午 1:32
 */
@BaseController
@Authorization
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;


    @ApiOperation(value = "查询用户信息", notes = "根据userID来获取用户详细信息")
    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    public ResultInfo<User> getUser(@CurrentUser User iuser) {
        User user = userService.getByUserInfor(iuser.getUserID());
        ResultInfo<User> resultInfo = new ResultInfo<User>();
        resultInfo.setMsg(ResultConstant.SUCCESS.getMessage());
        resultInfo.setData(user);
        resultInfo.setCode(ResultConstant.SUCCESS.getCode());
        return resultInfo;
    }

    @ApiOperation(value = "更新用户信息", notes = "根据用户名更新用户")
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public ResultInfo<User> updateUserByUserName(@RequestParam(value = "phone", required = false) String phone, @RequestParam(value = "userName", required = false) String userName, @RequestParam(value = "loginPwd", required = false) String loginPwd, @CurrentUser User iuser) {
        ResultInfo<User> resultInfo = new ResultInfo<>();
        User user = userService.getByUserId(iuser.getUserID());
        try {
            user.setUpdateUser(user.getUserID());
            user.setPhone(phone);
            user.setUserName(userName);
            user.setLoginPwd(loginPwd);
            userService.updateUser(user);
            resultInfo.setMsg(ResultConstant.SUCCESS.getMessage());
            resultInfo.setCode(ResultConstant.SUCCESS.getCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultInfo;
    }

    @ApiOperation(value = "更新用户信息", notes = "根据用户名更新用户")
    @RequestMapping(value = "/forgetPwd", method = RequestMethod.POST)
    public ResultInfo<User> forgetPwd(@RequestParam(value = "loginPwd", required = false) String loginPwd, @RequestParam(value = "phone", required = false) String phone, @RequestParam("validateCode") String code, @CurrentUser User iuser) {
        ResultInfo<User> resultInfo = new ResultInfo<>();
        try {
            //初始对象
            User user = new User();
            //取验证码
            String source = stringRedisTemplate.opsForValue().get(phone);
            //不为空
            if (source != null && source.equals(code)) {
                user.setUpdateUser(iuser.getUserID());
                user.setPhone(phone);
                user.setLoginPwd(loginPwd);
                userService.updatePwd(user);
                stringRedisTemplate.opsForValue().getOperations().delete(phone);
                resultInfo.setMsg(ResultConstant.SUCCESS.getMessage());
                resultInfo.setCode(ResultConstant.SUCCESS.getCode());
            } else {
                resultInfo.setMsg(ResultConstant.CHECK_CODE_FAIL.getMessage());
                resultInfo.setCode(ResultConstant.CHECK_CODE_FAIL.getCode());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultInfo;
    }

    @ApiOperation(value = "查询紧急联系人信息", notes = "查询紧急联系人信息")
    @RequestMapping(value = "getUrgentUser", method = RequestMethod.POST)
    public ResultInfo<User> getUrgentUser(@RequestBody UserEquipmentParameter userEquipmentParameter, @CurrentUser User iuser) {
        PageInfo<User> pageInfo = null;
        ResultInfo resultInfo = new ResultInfo();
        userEquipmentParameter.setUserId(iuser.getUserID());
        userEquipmentParameter.setOwn(TokenConstant.ZERO);
        pageInfo = userService.getUrgentUser(userEquipmentParameter);
        resultInfo.setMsg(ResultConstant.SUCCESS.getMessage());
        resultInfo.setData(pageInfo);
        resultInfo.setCode(ResultConstant.SUCCESS.getCode());
        return resultInfo;
    }

}
